package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class BlogSpotForwardPeer extends BasePeer{
	public static final String tableName = "BlogSpotForward";
	public static final String ID = "id";
	public static final String BLOGSPOTID = "blogSpotId";
	public static final String TARGETID = "targetId";
	public static final String APPID = "appId";
	public static final String[] columnNames=new String[]{"id","blogSpotId","targetId","appId"};
	public static BlogSpotForward getBlogSpotForwardByPKs(Object id, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotForwardPeer.tableName,BlogSpotForwardPeer.columnNames," TRUE  AND `id`=? ",new Object[]{id,},0, 1,conn);
			BlogSpotForward blogSpotForward = new BlogSpotForward();
		fill(rs,blogSpotForward);
		return blogSpotForward;
	}
	public static BlogSpotForward getBlogSpotForwardReferenceColumnsOnlyByPKs(Object id, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotForwardPeer.tableName,new String[]{"id",}," TRUE AND `id`=?",new Object[]{id,}, 0,  1,conn);
			BlogSpotForward blogSpotForward = new BlogSpotForward();
		fill(rs,blogSpotForward);
		return blogSpotForward;
	}
	public static BlogSpotForward getBlogSpotForwardById(long id, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotForwardPeer.tableName,BlogSpotForwardPeer.columnNames,"`id`=?",new Object[]{id},0, 1,conn);
			BlogSpotForward blogSpotForward = new BlogSpotForward();
		fill(rs,blogSpotForward);
		return blogSpotForward;
	}
	public static void fill(ResultSet rs, BlogSpotForward blogSpotForward) throws SQLException {
		if(rs.next()){
			try{blogSpotForward.setId(rs.getLong("id"));}catch(Exception ex){}
			try{blogSpotForward.setBlogSpotId(rs.getLong("blogSpotId"));}catch(Exception ex){}
			try{blogSpotForward.setTargetId(rs.getLong("targetId"));}catch(Exception ex){}
			try{blogSpotForward.setAppId(rs.getLong("appId"));}catch(Exception ex){}
			blogSpotForward.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<BlogSpotForward> blogSpotForwardCollection) throws SQLException {
		while(rs.next()){
			BlogSpotForward blogSpotForward=new BlogSpotForward();
			try{blogSpotForward.setId(rs.getLong("id"));}catch(Exception ex){}
			try{blogSpotForward.setBlogSpotId(rs.getLong("blogSpotId"));}catch(Exception ex){}
			try{blogSpotForward.setTargetId(rs.getLong("targetId"));}catch(Exception ex){}
			try{blogSpotForward.setAppId(rs.getLong("appId"));}catch(Exception ex){}
			blogSpotForward.resetModified();
			blogSpotForwardCollection.add(blogSpotForward);
		}
	}
	public static boolean update(BlogSpotForward blogSpotForward, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < BlogSpotForwardPeer.columnNames.length; i++) {
			if (((BlogSpotForwardBase)blogSpotForward).modified[i]) {
				columns.add(BlogSpotForwardPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `BlogSpotForward` SET ");
		sql.append("WHERE TRUE AND `id` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,blogSpotForward.get(column));
		}
		pstmt.setObject(columns.size()+1+0,blogSpotForward.get("id"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(BlogSpotForward blogSpotForward, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (blogSpotForward.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `BlogSpotForward` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (blogSpotForward.get(column) != null) {
				pstmt.setObject(i + 1 + j, blogSpotForward.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<BlogSpotForward> getBlogSpotForwardCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<BlogSpotForward> collection=new Vector<BlogSpotForward>();
		fill(rs,collection);
		return collection;
	}
}
